package com.mmy.controller.hotel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.hotel.THotelPlan;
import com.mmy.domain.hotel.THotelRoom;
import com.mmy.domain.hotel.VHotelBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.hotel.THotelBaseServiceImpl;
import com.mmy.service.impl.hotel.THotelPlanServiceImpl;
import com.mmy.service.impl.hotel.THotelRoomServiceImpl;
import com.mmy.service.impl.hotel.VHotelBaseServiceImpl;

@Controller
public class THotelPlanController {
	@Autowired
	THotelPlanServiceImpl tHotelPlanServiceImpl;
	@Autowired
	THotelBaseServiceImpl tHotelBaseServiceImpl;
	@Autowired
	VHotelBaseServiceImpl vHotelBaseServiceImpl;
	@Autowired
	THotelRoomServiceImpl tHotelRoomServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-11
	 * 
	 * 
	 */
	/* 进入创建餐馆游计划单页面 */
	@RequestMapping(value = "/tocreatehotelplan")
	public String toCreatHotelPlan(Model model) {

		/* 查询所有的酒店 */
		List<VHotelBase> lstHotelBase = vHotelBaseServiceImpl.findALL();
		model.addAttribute("newLstHotelBase", lstHotelBase);
		/* 查询所有的酒店 */
		List<THotelRoom> lstHotelRoom = tHotelRoomServiceImpl.findALL();
		model.addAttribute("newLstHotelRoom", lstHotelRoom);

		return "webbackground/hotelmanagement/createhotelplan";
	}

	/**
	 * 函数功能说明： 新建酒店计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/subcreatehotelplan")
	public String subCreateHotelPlan(THotelPlan tHotelPlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tHotelPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tHotelPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));

		// 获取餐馆游计划单最大值
		tHotelPlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_HOTEL_PLAN));

		try {

			tHotelPlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tHotelPlan.setCreator(tSysUser.getUserName());
			tHotelPlanServiceImpl.save(tHotelPlan.getPlanID());
			tHotelPlanServiceImpl.save(tHotelPlan);
			return "redirect:hotelplan";

		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/saveupdatehotelplan")
	public String saveUpdateHotelPlan(THotelPlan tHotelPlan, String startTime, String endTime, HttpSession session)
			throws Exception {
		try {
//			 定义一个新的实体类对象 
			THotelPlan dtHotelPlan = tHotelPlanServiceImpl.findByPlanID(tHotelPlan.getPlanID());
			dtHotelPlan.setService(tHotelPlan.getService());
			dtHotelPlan.setRoomID(tHotelPlan.getRoomID());
			dtHotelPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtHotelPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtHotelPlan.setFloor(tHotelPlan.getFloor());
			dtHotelPlan.setPeer(tHotelPlan.getPeer());
			dtHotelPlan.setPrice(tHotelPlan.getPrice());
			dtHotelPlan.setNumber(tHotelPlan.getNumber());
			dtHotelPlan.setExplain(tHotelPlan.getExplain());
			dtHotelPlan.setMemo(tHotelPlan.getMemo());
			dtHotelPlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtHotelPlan.setReviser(tSysUser.getUserName());
			tHotelPlanServiceImpl.save(dtHotelPlan);

			return "redirect:hotelplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除餐馆计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/deletehotelplanchecked")
	@ResponseBody
	public String deleteHotelPlanChecked(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		tHotelPlanServiceImpl.delHotelPlan(lstPlanID);

		return "webbackground/hotelmanagement/hotelplan";
	}

}
